<template>
	<view class="container">
		<view class="content">
			<view class="left">
				<view class="top-list">
					<view class="top-item" v-for="topData in list" :key="topData.id" 
						@click="changeTop(topData)">
						<view class="top">
							<view class="name">
								{{topData.h_name}}
							</view>
							<view class="amount" :class="topData.amount >= 0 ? 'amount-zheng' : 'amount-fu'">
								{{topData.balance}}元
							</view>
						</view>
						<view class="desc">
							{{typeList[topData.type]}}账户
						</view>
					</view>
					<view class="top-item" @click="toAdd"> 添加 </view>
				</view>
			</view>
		</view>
		<view class="bottom">
			<view class="bottom-btn btn-tc" v-if="editFlag" @click="editFlag = false">退出编辑</view>
			<view class="bottom-btn" v-else @click="editFlag = true">编辑</view>
		</view>
	</view>
</template>

<script>
	import {getSzaccount} from '@/api/ledger.js'
	export default {
		data() {
			return {
				list: [],
				typeList: {'1':'现金','2':'储蓄卡','3':'信用卡','4':'网络账户','5':'应收/应付'},
				editFlag: false,
				type:''
			}
		},
		onLoad(query) {
			this.type = query.type
		},
		onShow() {
			this.editFlag = false
			this.getSzaccount()
		},
		methods: {
			getSzaccount(){
				getSzaccount().then(res=>{
					this.list = res.data.data
				})
			},
			toAdd() {
				uni.navigateTo({
					url: '/pages-landlord/ledger/szaccountAdd'
				})
			},
			changeTop(data){
				if(this.editFlag) {
					uni.navigateTo({
						url: '/pages-landlord/ledger/szaccountEdit?id='+data.id
					})
				}else {
					uni.navigateBack({
							delta: 1,
							success: () => {
									// 通过事件或全局方式传参
									uni.$emit('szaccountSelected', {type:this.type,data:data});
							}
					});
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		background-color: #ffffff;
	}
	.content {
		position: relative;
		display: flex;
		height: calc(100vh - constant(safe-area-inset-bottom) - 80rpx)!important;
		height: calc(100vh - env(safe-area-inset-bottom) - 80rpx) !important;
	}
	.left {
		width: 100%;
		height: 100%;
		background-color: #f8f9fb;
		overflow-y: auto;
		.top-item {
			padding: 30rpx;
			font-size: 30rpx;
			font-weight: 700;
			border-bottom: 2rpx solid #f0eff4;
			.top {
				display: flex;
				justify-content: space-between;
			}
			.amount-zheng {
				color: #f26033;
			}
			.amount-fu {
				color: #10cc00;
			}
			.desc {
				color: #978888;
				font-weight: normal;
			}
		}
	}
	
	.bottom {
		width: 100vw;
		height: calc(constant(safe-area-inset-bottom) + 80rpx);
		height: calc(env(safe-area-inset-bottom) + 80rpx);
		border-top: 1rpx solid #f0eff4;
		background-color: #f8f9fb;
	}
	.bottom-btn {
		padding: 20rpx;
		padding-bottom: calc(constant(safe-area-inset-bottom) + 20rpx)!important;
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx) !important;
	}
	.btn-tc {
		width: 100%;
		text-align: center;
		background-color: #ffc107;
		color: #fff;
		font-weight: 700;
	}
</style>